package com.example.serviceheadlines.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.serviceheadlines.mapper.HeadlinesThumpMapper;
import com.example.serviceheadlines.service.HeadlinesService;
import com.example.serviceheadlines.service.IHeadlinesCommentService;
import com.example.serviceheadlines.service.IHeadlinesThumpService;
import com.tangyuan.common.result.Result;
import com.tangyuan.common.result.ResultCodeEnum;
import com.tangyuan.model.Headlines;
import com.tangyuan.model.HeadlinesComment;
import com.tangyuan.model.HeadlinesThump;
import com.tangyuan.model.UserHeadlinesCollection;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @Description: headlines_thump
 * @Author: jeecg-boot
 * @Date: 2024-03-18
 * @Version: V1.0
 */
@Service
@Slf4j
public class HeadlinesThumpServiceImpl extends ServiceImpl<HeadlinesThumpMapper, HeadlinesThump> implements IHeadlinesThumpService {


    @Autowired
    private HeadlinesService headlinesService;
    @Autowired
    private IHeadlinesCommentService headlinesCommentService;

    @Autowired
    private HeadlinesThumpMapper headersThumpMapper;

    /**
     * 百家点赞
     *
     * @param headlinesId
     * @param userId
     * @return
     */
    @Transactional
    @Override
    public Result addCommentThumpById(Long headlinesId, Long userId) {
        Headlines headlines = headlinesService.getById(headlinesId);
        if (headlines == null) {
            return Result.build(null, ResultCodeEnum.NO_DATA);
        }
        LambdaQueryWrapper<HeadlinesThump> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HeadlinesThump::getUserId, userId);
        queryWrapper.eq(HeadlinesThump::getAId, headlinesId);
        queryWrapper.eq(HeadlinesThump::getIsThump, 0);
        queryWrapper.eq(HeadlinesThump::getIsDelete, 0);
        HeadlinesThump headlinesThump = this.getOne(queryWrapper);
        if (headlinesThump == null) {
            headlines.setThumpUpNum(headlines.getThumpUpNum() + 1);
            headlinesThump = new HeadlinesThump();
            headlinesThump.setAId(headlinesId);
            headlinesThump.setUserId(userId);
            headlinesThump.setCreateTime(new Date());
            this.save(headlinesThump);
        } else {
            headlines.setThumpUpNum(headlines.getThumpUpNum() - 1);
            this.removeById(headlinesThump.getId());
        }
        headlinesService.updateById(headlines);
        return Result.ok("操作成功！");
    }

    @Override
    public Result addCommentThump(Long commentId, Long userId) {
        HeadlinesComment headlinesComment = headlinesCommentService.getById(commentId);
        if (headlinesComment == null) {
            return Result.build(null,ResultCodeEnum.NO_DATA);
        }
        LambdaQueryWrapper<HeadlinesThump> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HeadlinesThump::getUserId, userId);
        queryWrapper.eq(HeadlinesThump::getAId, commentId);
        queryWrapper.eq(HeadlinesThump::getIsThump, 1);
        queryWrapper.eq(HeadlinesThump::getIsDelete, 0);
        HeadlinesThump headlinesThump = this.getOne(queryWrapper);

        if (headlinesThump == null) {
            headlinesComment.setThumpUpNum(headlinesComment.getThumpUpNum() + 1);
            headlinesThump = new HeadlinesThump();
            headlinesThump.setAId(commentId);
            headlinesThump.setUserId(userId);
            headlinesThump.setIsThump(1);
            headlinesThump.setCreateTime(new Date());
            this.save(headlinesThump);
        } else {
            headlinesComment.setThumpUpNum(headlinesComment.getThumpUpNum() - 1);
            this.removeById(headlinesThump.getId());
        }
        headlinesCommentService.updateById(headlinesComment);
        return Result.ok("操作成功！");
    }

    /**
     * 用户不喜欢评论及取消不喜欢
     *
     * @param commentId
     * @return
     */
    @Override
    public Result<Object> addNoLike(Long commentId, Long userId) {
        LambdaQueryWrapper<HeadlinesThump> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HeadlinesThump::getUserId, userId);
        queryWrapper.eq(HeadlinesThump::getAId, commentId);
       // queryWrapper.eq(HeadlinesThump::getIsThump, 1);
        queryWrapper.eq(HeadlinesThump::getIsLike, 1);
        queryWrapper.eq(HeadlinesThump::getIsDelete, 0);
        HeadlinesThump headlinesThump = this.getOne(queryWrapper);
        HeadlinesComment headlinesComment = headlinesCommentService.getById(commentId);
        if (headlinesComment == null) {
            return Result.build(null,ResultCodeEnum.NO_DATA);
        }
        if (headlinesThump == null) {
            headlinesComment.setDislikeCount(headlinesComment.getDislikeCount() + 1);
            headlinesThump = new HeadlinesThump();
            headlinesThump.setAId(commentId);
            headlinesThump.setUserId(userId);
         //   headlinesThump.setIsThump(1);
            headlinesThump.setIsLike(1);
            headlinesThump.setCreateTime(new Date());
            this.save(headlinesThump);
        } else {
            headlinesComment.setDislikeCount(headlinesComment.getDislikeCount() - 1);
            this.removeById(headlinesThump.getId());
        }
        headlinesCommentService.updateById(headlinesComment);
        return Result.ok("操作成功！");
    }

    @Override
    public HeadlinesThump getHeadlinesThumpOne(Long userId, Integer articleId) {
        return headersThumpMapper.getHeadlinesThumpOne(userId,articleId);
    }

    @Override
    public List<HeadlinesThump> getMyTrumpHeadlines(Long userId, Integer pageNum, Integer pageSize) {
        QueryWrapper<HeadlinesThump> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(HeadlinesThump::getUserId,userId);
        queryWrapper.lambda().orderByDesc(HeadlinesThump::getCreateTime);
        return baseMapper.selectPage(new Page<>(pageNum,pageSize),queryWrapper).getRecords();
    }

    @Override
    public void deleteHeadlines(List<Long> headlines) {
        if(CollectionUtils.isEmpty(headlines)){
            log.info("百家点赞数据删除为空");
            return;
        }
        baseMapper.delete(new QueryWrapper<HeadlinesThump>()
                .lambda().in(HeadlinesThump::getAId,headlines));
    }
}
